package org.fmi.lje.loader.impl;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

import org.fmi.lje.loader.ProjectLoader;
import org.fmi.lje.model.UmlGenProject;
import org.fmi.lje.pp.classpath.ClasspathResolver;

import com.google.inject.Inject;

public class ProjectLoaderImpl implements ProjectLoader {
	
	private ClasspathResolver classpathResolver;

	@Inject
	public ProjectLoaderImpl(ClasspathResolver classpathResolver) {
		this.classpathResolver = classpathResolver;
	}

	@Override
	public ClassLoader loadProject(UmlGenProject project) {
		List<URL> all = classpathResolver.getClasspath(project);
		
		String projectFolder = project.getProjectFolder();
		File destDir = new File(projectFolder  + File.separator + "umlgen-output");
		
		try {
			all.add(destDir.toURI().toURL());
		} catch (MalformedURLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		
		return new URLClassLoader(all.toArray(new URL[all.size()]));
	}

}
